<?php
//define('BASEURL','http://localhost/testsqlite/template/index-test.php');
    include 'config.php';
    
    //BLOCK HEADER
    $sql_header = "select * from header where status = 1";
    $result_header = $db->query($sql_header);
    $row_header = $result_header->fetchArray(SQLITE3_ASSOC);
    $rowNameHeader = BASE_URL.'/images/banner/' .$row_header['url_header'];
    
    $tpl->assign('URL_HEADER',$rowNameHeader);
    $tpl->assign('BASEURL',BASE_URL);
    $tpl->parse('main.header');
    
    //BLOCK LOAITIN
    $sql_loaitin = "select * from loaitin";
    $result = $db->query($sql_loaitin);
    while($row = $result->fetchArray(SQLITE3_ASSOC))
    {
        $rowName = $row['tenloaitin'];    
        $tpl->assign('LOAITIN',$rowName);
        $tpl->parse('main.loaitin');
    }
    
	
    
    
    //BLOCK CHITIETBAIVIET
	if(isset($_GET['xem']) && $_GET['xem'] == 'chitietbaiviet')
	{
		$id = $_GET['id'];
		$sql_chitietbv = "select * from baiviet where IDbaiviet = '$id'";
		$row_chitietbv = $db->query($sql_chitietbv)->fetchArray();
		$arr_chitietbv = array(1=>array('ANHBAIVIET'=>BASE_URL. 'images/anhbaiviet/'. $row_chitietbv['anhminhhoa'].'',
										 'TIEUDE_BAIVIET'=>''.$row_chitietbv['tieudebaiviet'].'',
                                         'NGAYDANG'=>''.$row_chitietbv['ngaydang'].'',
                                         'TOMTAT_BAIVIET'=>''.$row_chitietbv['tomtat'].'',
                                         'NOIDUNG_BAIVIET'=>''.$row_chitietbv['noidung'].'',
                                         ));
         $tpl->assign('NOIDUNG',$arr_chitietbv[1]);
         $tpl->parse('main.post_chitiet');
	} else {
		//BLOCK BAIVIET
		$sql_baiviet = "select * from baiviet";
	    $result_baiviet = $db->query($sql_baiviet);
	    $sobaiviet = 0;
	    while($row_baiviet = $result_baiviet->fetchArray(SQLITE3_ASSOC))
		{
		    $rowTomtatBaiviet = $row_baiviet['tomtat'];
		    $rowNgaydang = $row_baiviet['ngaydang'];
		    $rowTieudeBaiviet = $row_baiviet['tieudebaiviet'];
		    $rowAnhbaiviet = BASE_URL. 'images/anhbaiviet/' .$row_baiviet['anhminhhoa'];
		    $description = $row_baiviet['tomtat'];
	        $subTieude = $row_baiviet['tieudebaiviet'];
	        
	        //SUBSTRING TOMTAT
	        if(strlen($description) > 200 ){
	             $rowTomtatBaiviet= substr($description,0,strpos($description,' ',200)).'...';
	        }
	        //SUBSTRING TIEUDE
	        if(strlen($subTieude) > 60 ){
	             $rowTieudeBaiviet= substr($subTieude,0,strpos($subTieude,' ',60)).'...';
	        }
	        $tpl->assign('URL_BAIVIET','index-test.php?xem=chitietbaiviet&id='.$row_baiviet['IDbaiviet']);
		    $tpl->assign('TOMTAT_BAIVIET',$rowTomtatBaiviet);
		    $tpl->assign('NGAYDANG',$rowNgaydang);
		    $tpl->assign('TIEUDE_BAIVIET',$rowTieudeBaiviet);
		    $tpl->assign('ANHBAIVIET',$rowAnhbaiviet);
		    $tpl->parse('main.post');
		}
		
		
	}
	//BLOCK BAAIVIETDACBIET
		$sql_baivietdacbiet = "select * from BAIVIET where baivietdacbiet = 1 ORDER BY ngaydang";
	    $result_baivietdacbiet = $db->query($sql_baivietdacbiet);
	    
	    while($row_baivietdacbiet = $result_baivietdacbiet->fetchArray(SQLITE3_ASSOC))
		{
			$subTieudedacbiet = $row_baivietdacbiet['tieudebaiviet'];
		    $rowTieudeBaivietdacbiet = $row_baivietdacbiet['tieudebaiviet'];
		    $rowAnhbaivietdacbiet = BASE_URL. 'images/anhbaiviet/' .$row_baivietdacbiet['anhminhhoa'];
		    //SUBSTRING TOMTAT
	         
	        $tpl->assign('TIEUDE_BAIVIET',$rowTieudeBaivietdacbiet);
		    $tpl->assign('ANHBAIVIET',$rowAnhbaivietdacbiet);
		    $tpl->parse('main.post_dacbiet');
		}
	
	 $tpl->parse('main.footer');
	 $tpl->parse('main');
	 $tpl->out('main');
	 
?>
